package cc.rengu.igas.momp.biz.service.impl;

import cc.rengu.igas.momp.common.enums.MompTxnNumEnum;
import cc.rengu.igas.momp.common.enums.RespCodeEnum;
import cc.rengu.igas.momp.facade.QueryAvailableDiscountInfoService;
import cc.rengu.igas.momp.facade.request.OpmsQueryAvailableDiscountInfoBeforePaymentRequest;
import cc.rengu.igas.momp.facade.request.QueryAvailableDiscountInfoAfterPaymentRequest;
import cc.rengu.igas.momp.facade.request.QueryAvailableDiscountInfoBeforePaymentRequest;
import cc.rengu.igas.momp.facade.response.OpmsQueryAvailableDiscountInfoBeforePaymentResponse;
import cc.rengu.igas.momp.facade.response.QueryAvailableDiscountInfoAfterPaymentResponse;
import cc.rengu.igas.momp.facade.response.QueryAvailableDiscountInfoBeforePaymentResponse;
import cc.rengu.igas.momp.facade.result.Result;
import cc.rengu.oltp.service.common.enums.OltpRpcdEnum;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.DubboResult;
import cc.rengu.oltp.service.realize.DubboService;
import cc.rengu.oltp.service.realize.impl.DubboServiceImpl;

/**
 * 可优惠信息查询接口实现
 *
 * @author Jinan Liu
 * @version 1.0.0
 * @date 2020/3/24 11:54
 */
@SuppressWarnings("unchecked")
public class QueryAvailableDiscountInfoServiceImpl implements QueryAvailableDiscountInfoService {

    /**
     * 支付前可优惠信息查询接口Dubbo实现
     *
     * @param queryAvailableDiscountInfoBeforePaymentRequest 支付前可优惠信息查询接口请求对象
     * @return cc.rengu.igas.momp.facade.result.Result<cc.rengu.igas.momp.facade.response.QueryAvailableDiscountInfoBeforePaymentResponse> 支付前可优惠信息查询接口应答对象
     * @author Jinan Liu
     * @date 2020/6/3 14:25
     */
    @Override
    public Result<QueryAvailableDiscountInfoBeforePaymentResponse> queryAvailableDiscountInfoBeforePayment(QueryAvailableDiscountInfoBeforePaymentRequest queryAvailableDiscountInfoBeforePaymentRequest) {

        DubboService<QueryAvailableDiscountInfoBeforePaymentRequest> dubboService = new DubboServiceImpl();
        Result<QueryAvailableDiscountInfoBeforePaymentResponse> queryAvailableDiscountInfoBeforePaymentResponseResult = new Result<>();
        try {
            if (!MompTxnNumEnum.AVAIL_DISCOUNT_PRE_QUERY.getTxnNum().equals(queryAvailableDiscountInfoBeforePaymentRequest.getHeader().getTxnNum())) {
                throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
            }
            // 通过服务名调用相应的服务
            DubboResult<QueryAvailableDiscountInfoBeforePaymentResponse> dubboResult = dubboService.callAtomService(queryAvailableDiscountInfoBeforePaymentRequest,
                    MompTxnNumEnum.AVAIL_DISCOUNT_PRE_QUERY.getService(), QueryAvailableDiscountInfoBeforePaymentResponse.class);
            queryAvailableDiscountInfoBeforePaymentResponseResult.setResult(dubboResult.getResult());
            queryAvailableDiscountInfoBeforePaymentResponseResult.setSuccess(dubboResult.isSuccess());

        } catch (Exception e) {

            QueryAvailableDiscountInfoBeforePaymentResponse queryAvailableDiscountInfoBeforePaymentResponse = new QueryAvailableDiscountInfoBeforePaymentResponse();
            queryAvailableDiscountInfoBeforePaymentResponseResult.setResult(queryAvailableDiscountInfoBeforePaymentResponse);
            queryAvailableDiscountInfoBeforePaymentResponse.setHeader(queryAvailableDiscountInfoBeforePaymentRequest.getHeader());
            queryAvailableDiscountInfoBeforePaymentResponseResult.setSuccess(false);
            if (e instanceof BizException) {
                queryAvailableDiscountInfoBeforePaymentResponse.setRespCode(((BizException) e).getCode());
                queryAvailableDiscountInfoBeforePaymentResponse.setRespDesc(e.getMessage());
            } else {
                queryAvailableDiscountInfoBeforePaymentResponse.setRespCode(OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespCode());
                queryAvailableDiscountInfoBeforePaymentResponse.setRespDesc(OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespDesc());
            }
        }

        return queryAvailableDiscountInfoBeforePaymentResponseResult;
    }

    /**
     * 支付成功可优惠信息查询接口Dubbo实现
     *
     * @param queryAvailableDiscountInfoAfterPaymentRequest 支付成功可优惠信息查询接口请求对象
     * @return cc.rengu.igas.momp.facade.result.Result<cc.rengu.igas.momp.facade.response.QueryAvailableDiscountInfoAfterPaymentResponse> 支付成功可优惠信息查询接口应答对象
     * @author Jinan Liu
     * @date 2020/6/3 14:26
     */
    @Override
    public Result<QueryAvailableDiscountInfoAfterPaymentResponse> queryAvailableDiscountInfoAfterPayment(QueryAvailableDiscountInfoAfterPaymentRequest queryAvailableDiscountInfoAfterPaymentRequest) {

        DubboService<QueryAvailableDiscountInfoAfterPaymentRequest> dubboService = new DubboServiceImpl();
        Result<QueryAvailableDiscountInfoAfterPaymentResponse> queryAvailableDiscountInfoAfterPaymentResponseResult = new Result<>();
        try {
            if (!MompTxnNumEnum.AVAIL_DISCOUNT_AFTER_QUERY.getTxnNum().equals(queryAvailableDiscountInfoAfterPaymentRequest.getHeader().getTxnNum())) {
                throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
            }
            // 通过服务名调用相应的服务
            DubboResult<QueryAvailableDiscountInfoAfterPaymentResponse> dubboResult = dubboService.callAtomService(queryAvailableDiscountInfoAfterPaymentRequest,
                    MompTxnNumEnum.AVAIL_DISCOUNT_AFTER_QUERY.getService(), QueryAvailableDiscountInfoAfterPaymentResponse.class);
            queryAvailableDiscountInfoAfterPaymentResponseResult.setResult(dubboResult.getResult());
            queryAvailableDiscountInfoAfterPaymentResponseResult.setSuccess(dubboResult.isSuccess());

        } catch (Exception e) {

            QueryAvailableDiscountInfoAfterPaymentResponse queryAvailableDiscountInfoAfterPaymentResponse = new QueryAvailableDiscountInfoAfterPaymentResponse();
            queryAvailableDiscountInfoAfterPaymentResponseResult.setResult(queryAvailableDiscountInfoAfterPaymentResponse);
            queryAvailableDiscountInfoAfterPaymentResponse.setHeader(queryAvailableDiscountInfoAfterPaymentRequest.getHeader());
            queryAvailableDiscountInfoAfterPaymentResponseResult.setSuccess(false);
            if (e instanceof BizException) {
                queryAvailableDiscountInfoAfterPaymentResponse.setRespCode(((BizException) e).getCode());
                queryAvailableDiscountInfoAfterPaymentResponse.setRespDesc(e.getMessage());
            } else {
                queryAvailableDiscountInfoAfterPaymentResponse.setRespCode(OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespCode());
                queryAvailableDiscountInfoAfterPaymentResponse.setRespDesc(OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespDesc());
            }
        }

        return queryAvailableDiscountInfoAfterPaymentResponseResult;
    }

    /**
     * 收银台支付前可优惠信息查询接口Dubbo实现
     *
     * @param opmsQueryAvailableDiscountInfoBeforePaymentRequest 收银台支付前可优惠信息查询接口请求对象
     * @return cc.rengu.igas.momp.facade.result.Result<cc.rengu.igas.momp.facade.response.QueryAvailableDiscountInfoBeforePaymentResponse> 支付前可优惠信息查询接口应答对象
     * @author liujinan
     * @date 2021/6/16 14:25
     */
    @Override
    public Result<OpmsQueryAvailableDiscountInfoBeforePaymentResponse> queryAvailableDiscountInfoBeforePaymentForOpms(OpmsQueryAvailableDiscountInfoBeforePaymentRequest opmsQueryAvailableDiscountInfoBeforePaymentRequest) {

        DubboService<OpmsQueryAvailableDiscountInfoBeforePaymentRequest> dubboService = new DubboServiceImpl();
        Result<OpmsQueryAvailableDiscountInfoBeforePaymentResponse> opmsQueryAvailableDiscountInfoBeforePaymentResponseResult = new Result<>();
        try {
            if (!MompTxnNumEnum.OPMS_AVAIL_DISCOUNT_PRE_QUERY.getTxnNum().equals(opmsQueryAvailableDiscountInfoBeforePaymentRequest.getHeader().getTxnNum())) {
                throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
            }
            // 通过服务名调用相应的服务
            DubboResult<OpmsQueryAvailableDiscountInfoBeforePaymentResponse> dubboResult = dubboService.callAtomService(opmsQueryAvailableDiscountInfoBeforePaymentRequest,
                    MompTxnNumEnum.OPMS_AVAIL_DISCOUNT_PRE_QUERY.getService(), OpmsQueryAvailableDiscountInfoBeforePaymentResponse.class);
            opmsQueryAvailableDiscountInfoBeforePaymentResponseResult.setResult(dubboResult.getResult());
            opmsQueryAvailableDiscountInfoBeforePaymentResponseResult.setSuccess(dubboResult.isSuccess());

        } catch (Exception e) {

            OpmsQueryAvailableDiscountInfoBeforePaymentResponse opmsQueryAvailableDiscountInfoBeforePaymentResponse = new OpmsQueryAvailableDiscountInfoBeforePaymentResponse();
            opmsQueryAvailableDiscountInfoBeforePaymentResponseResult.setResult(opmsQueryAvailableDiscountInfoBeforePaymentResponse);
            opmsQueryAvailableDiscountInfoBeforePaymentResponse.setHeader(opmsQueryAvailableDiscountInfoBeforePaymentRequest.getHeader());
            opmsQueryAvailableDiscountInfoBeforePaymentResponseResult.setSuccess(false);
            if (e instanceof BizException) {
                opmsQueryAvailableDiscountInfoBeforePaymentResponse.setRespCode(((BizException) e).getCode());
                opmsQueryAvailableDiscountInfoBeforePaymentResponse.setRespDesc(e.getMessage());
            } else {
                opmsQueryAvailableDiscountInfoBeforePaymentResponse.setRespCode(OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespCode());
                opmsQueryAvailableDiscountInfoBeforePaymentResponse.setRespDesc(OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespDesc());
            }
        }

        return opmsQueryAvailableDiscountInfoBeforePaymentResponseResult;
    }
}
